package model;

import view.Boundary;
import cards.Card;
import fields.Blank;
import fields.Brewery;
import fields.Chance;
import fields.Field;
import fields.Jail;
import fields.Refuge;
import fields.Shipping;
import fields.Street;
import fields.Tax;
import gui.G;

public class Board {
	public static final int JAILPOSITION = 11;
	public static final int GOTOJAILPOSITION = 31;
	private static final int STARTBONUS = 4000;
	private static final int GETOUTOFJAILPRICE = 1000;
	private static final int INITBALANCE = 30000;
	
	public static int startBonus = STARTBONUS;
	public static int getOutOfJailPrice = GETOUTOFJAILPRICE;
	public static int initBalance = INITBALANCE;
	
	public static Field[] fields = new Field[40];
	public static Player[] players = new Player[6];
	public static Cup cup = new Cup();
	public static Deck deck = new Deck();
	public static Card nextCard;

	private String getFieldString() {
		return "Type,Felt,Navn,Pris,Leje,1 hus,2 huse,3 huse,4 huse,hotel,huspris\r\n"
			+ "Blank,1,Start,,,,,,,,\r\n"
			+ "Street,2,Rødovrevej,1200,50,250,750,2250,4000,6000,1000\r\n"
			+ "Chance,3,Prøv lykken,,,,,,,,\r\n"
			+ "Street,4,Hvidovrevej,1200,50,250,750,2250,4000,6000,1000\r\n"
			+ "Tax,5,Betal indkomstskat,10% el. 4000,4000,10,,,,,\r\n"
			+ "Shipping,6,LB Færgerne,4000,500,,,,,,\r\n"
			+ "Street,7,Roskildevej,2000,100,600,1800,5400,8000,11000,1000\r\n"
			+ "Chance,8,Prøv lykken,,,,,,,,\r\n"
			+ "Street,9,Valby Langgade,2000,100,600,1800,5400,8000,11000,1000\r\n"
			+ "Street,10,Allégade,2400,150,800,2000,6000,9000,12000,1000\r\n"
			+ "Blank,11,Fængsel,,,,,,,,\r\n"
			+ "Street,12,Frederiks-<br>berg Allé,2800,200,1000,3000,9000,12500,15000,2000\r\n"
			+ "Brewery,13,Tuborg,3000,100,,,,,,\r\n"
			+ "Street,14,Bülowsvej,2800,200,1000,3000,9000,12500,15000,2000\r\n"
			+ "Street,15,Gl. Kongevej,3200,250,1250,3750,10000,14000,18000,2000\r\n"
			+ "Shipping,16,Danmark,4000,500,,,,,,\r\n"
			+ "Street,17,Bernstorfs-<br>vej,3600,300,1400,4000,11000,15000,19000,2000\r\n"
			+ "Chance,18,Prøv lykken,,,,,,,,\r\n"
			+ "Street,19,Hellerupvej,3600,300,1400,4000,11000,15000,19000,2000\r\n"
			+ "Street,20,Strandvej,4000,350,1600,4400,12000,16000,20000,2000\r\n"
			+ "Refuge,21,Parkering,5000,,,,,,,\r\n"
			+ "Street,22,Trianglen,4400,350,1800,5000,14000,17500,21000,3000\r\n"
			+ "Chance,23,Prøv lykken,,,,,,,,\r\n"
			+ "Street,24,Østerbro-<br>gade,4400,350,1800,5000,14000,17500,21000,3000\r\n"
			+ "Street,25,Grønningen,4800,400,2000,6000,15000,18500,22000,3000\r\n"
			+ "Shipping,26,Mols-Linien,4000,500,,,,,,\r\n"
			+ "Street,27,Bredgade,5200,450,2200,6600,16000,19500,23000,3000\r\n"
			+ "Street,28,Kgs. Nytorv,5200,450,2200,6600,16000,19500,23000,3000\r\n"
			+ "Brewery,29,Carlsberg,3000,200,,,,,,\r\n"
			+ "Street,30,Østergade,5600,500,2400,7200,17000,20500,24000,3000\r\n"
			+ "Jail,31,De Fængsles,,,,,,,,\r\n"
			+ "Street,32,Amagertorv,6000,550,2600,7800,18000,22000,25000,4000\r\n"
			+ "Street,33,Vimmel-<br>skaftet,6000,550,2600,7800,18000,22000,25000,4000\r\n"
			+ "Chance,34,Prøv lykken,,,,,,,,\r\n"
			+ "Street,35,Nygade,6400,600,3000,9000,20000,24000,28000,4000\r\n"
			+ "Shipping,36,Skandinavisk,4000,500,,,,,,\r\n"
			+ "Chance,37,Prøv lykken,,,,,,,,\r\n"
			+ "Street,38,Frederiks-<br>berggade,7000,700,3500,10000,22000,26000,30000,4000\r\n"
			+ "Tax,39,Ekstra-<br>ordinær statsskat,Betal 2000,2000,,,,,,\r\n"
			+ "Street,40,Rådhus-<br>pladsen,8000,1000,4000,12000,28000,34000,40000,4000\r\n";
	}
	public Board() {
		nextCard = Deck.draw();
		Boundary.setChanceCard(nextCard.getText());
		
		String all = this.getFieldString();
		for (int i = 1; i < all.split("\r\n").length; i++) {
			String line = all.split("\r\n")[i];
			String type = line.split(",")[0];
			int value = 0;
			for(char c : type.toCharArray()) value += c;
			
			//Silly java(1.6)!
			switch (value) {
			case 'B'+'l'+'a'+'n'+'k': makeBlank(line); break;
			case 'B'+'r'+'e'+'w'+'e'+'r'+'y': makeBrewery(line); break;
			case 'C'+'h'+'a'+'n'+'c'+'e': makeChance(line); break;
			case 'J'+'a'+'i'+'l': makeJail(line); break;
			case 'R'+'e'+'f'+'u'+'g'+'e': makeRefuge(line); break;
			case 'S'+'h'+'i'+'p'+'p'+'i'+'n'+'g': makeShipping(line); break;
			case 'S'+'t'+'r'+'e'+'e'+'t': makeStreet(line); break;
			case 'T'+'a'+'x': makeTax(line); break;
			}
		}
	}
	
	private void makeBlank(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		Board.fields[number - 1] =
				new Blank(number, name);
	}
	
	private void makeBrewery(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		int price = Integer.parseInt(tokens[3]);
		int baseRent = Integer.parseInt(tokens[4]);
		Board.fields[number - 1] = 
				new Brewery(number, name, price, baseRent);
	}

	private void makeChance(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		Board.fields[number - 1] = 
				new Chance(number, name);
	}

	private void makeJail(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		Board.fields[number - 1] = 
				new Jail(number, name);
	}

	private void makeRefuge(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		int bonus = Integer.parseInt(tokens[3]);
		Board.fields[number - 1] = 
				new Refuge(number, name, bonus);
	}

	private void makeShipping(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		int price = Integer.parseInt(tokens[3]);
		int baseRent = Integer.parseInt(tokens[4]);
		Board.fields[number - 1] = 
				new Shipping(number, name, price, baseRent);
	}
	
	private void makeStreet(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		int price = Integer.parseInt(tokens[3]);
		int housePrice = Integer.parseInt(tokens[10]);
		int[] rents = {Integer.parseInt(tokens[4]), //No houses
					   Integer.parseInt(tokens[5]), //1 house
					   Integer.parseInt(tokens[6]), //2 houses
					   Integer.parseInt(tokens[7]), //3 houses
					   Integer.parseInt(tokens[8]), //4 houses
					   Integer.parseInt(tokens[9]),}; //Hotel
		Board.fields[number - 1] = 
				new Street(number, name, price, housePrice, rents);
	}
	
	private void makeTax(String line) {
		String[] tokens = line.split(",");
		int number = Integer.parseInt(tokens[1]);
		String name = tokens[2];
		String displayText = tokens[3];
		int taxAmount = Integer.parseInt(tokens[4]);
		if(tokens.length >= 6){
			int taxPercentage = Integer.parseInt(tokens[5]);
			Board.fields[number - 1] = 
					new Tax(number, name, taxAmount, taxPercentage, displayText);
		}else{
			Board.fields[number - 1] = 
					new Tax(number, name, taxAmount, displayText);
		}
	}

	@Override
	public String toString(){
		String out = "";
		for(Field f : fields){
			out += f.toString() + "\n";
		}
		return out;
	}
}